﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace TTBox
{
    class StoreData
    {
        ArrayList m_alstDispatch;
        ArrayList m_alstMsg;
        ArrayList m_alstReserveJob;
        Hashtable m_hashConfig;

        static int StoreCountMax = 200;

        static private StoreData m_Instance = new StoreData();
        private StoreData()
        {
            initial();
        }

        static public StoreData getInstance()
        {
            return m_Instance;
        }

        public void initial()
        {
            loadDispatch();
            loadMsg();
            loadReserveJob();
            loadConfig();

        }


        #region Record
        private void saveDispatch() { saveRec(m_alstDispatch, "DRec.txt"); }
        private void loadDispatch(){ loadRec(ref m_alstDispatch,"DRec.txt");}

        private void saveReserveJob() { saveRec(m_alstReserveJob, "RRec.txt"); }
        private void loadReserveJob() { loadRec(ref m_alstReserveJob, "RRec.txt"); }

        private void saveMsg() { saveRec(m_alstMsg, "MRec.txt"); }
        private void loadMsg() { loadRec(ref m_alstMsg, "MRec.txt"); }



        public void addDispatch(string arg_strData)
        {
            m_alstDispatch.Insert(0, arg_strData);
            if (m_alstDispatch.Count > StoreCountMax)
                m_alstDispatch.RemoveAt(StoreCountMax-1);
            saveDispatch();
        }
        public void delDispatch(int arg_nIndex)
        {
            LogManager.getInstance().Log("DD:" + arg_nIndex.ToString());

            if (arg_nIndex >= 0 && m_alstDispatch.Count > arg_nIndex )
                m_alstDispatch.RemoveAt(arg_nIndex);
            saveDispatch();
        }
        public ArrayList getDispatch()
        {
            return (ArrayList)m_alstDispatch.Clone();
        }

        public void addReserveJob(string arg_strData)
        {
            m_alstReserveJob.Insert(0, arg_strData);
            if (m_alstReserveJob.Count > StoreCountMax)
                m_alstReserveJob.RemoveAt(StoreCountMax-1);
            saveReserveJob();
        }

        public string checkReserveJobCancel(string arg_strOrderID)
        {
            try
            {
                ArrayList alJob = getReserveJob();
                LogManager.getInstance().Log("check RJob count :" + alJob.Count.ToString() + " with " + arg_strOrderID);

                for (int i = 0; i < alJob.Count; i++)
                {
                    string strJob = (string)alJob[i];
                    LogManager.getInstance().Log("check RJob:" + strJob.Substring(5, arg_strOrderID.Length) + " ~ " + arg_strOrderID);
                    if (strJob.Substring(5,arg_strOrderID.Length) == arg_strOrderID)
                    {
                        delReserveJob(i);
                        return strJob;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return "";
        }

        public void delReserveJob(int arg_nIndex)
        {
            if (arg_nIndex >= 0 && m_alstReserveJob.Count > arg_nIndex)
                m_alstReserveJob.RemoveAt(arg_nIndex);
            saveReserveJob();
        }

        public int delReserveJob(string arg_strData)
        {
            int nDel = -1;
            for (int i = 0; i < m_alstReserveJob.Count; i++)
            {
                if (((string)m_alstReserveJob[i]).IndexOf(arg_strData) != -1)
                {
                    nDel = i;
                    break;
                }
            }
            
            if (nDel != -1)
                delReserveJob(nDel);
            return nDel;
        }

        public ArrayList getReserveJob()
        {
            return (ArrayList)m_alstReserveJob.Clone();
        }
        public SortedList getReserveJobBySorted()
        {
            ArrayList alJob = getReserveJob();
            SortedList sl = new SortedList();
            string strData = "";
            for (int i = 0; i < alJob.Count; i++)
            {
                try
                {
                    string strJob = (string)alJob[i];
                    strData += strJob + "\t";
                    int nLoc = strJob.IndexOf('/');
                    string strDT = strJob.Substring(nLoc - 2, 11);
                    strDT = strDT.Replace("//", "").Replace(":", "");

                    int nMM = System.Convert.ToInt32(strDT.Substring(0, 2));
                    int nyy = System.DateTime.Now.Year;

                    if (nMM < System.DateTime.Now.Month)
                        nyy += 1;

                    sl.Add(nyy.ToString() + strDT, strJob);

                }
                catch (Exception ex)
                {
                    LogManager.getInstance().Log("getReserveJobBySorted:" + strData, ex);
                }
            }

            return sl;
        }

        public void addMsg(string arg_strData)
        {
            m_alstMsg.Insert(0, arg_strData);
            if (m_alstMsg.Count > StoreCountMax)
                m_alstMsg.RemoveAt(StoreCountMax-1);
            saveMsg();
        }
        public void delMsg(int arg_nIndex)
        {
            LogManager.getInstance().Log("DM:" + arg_nIndex.ToString());

            if (arg_nIndex >= 0 && m_alstMsg.Count > arg_nIndex)
                m_alstMsg.RemoveAt(arg_nIndex);
            saveMsg();
        }


        public ArrayList getMsg()
        {
            return (ArrayList)m_alstMsg.Clone();
        }



        private void saveRec(ArrayList arg_al , string arg_strFileName)
        {
            TextWriter tw = new StreamWriter(Form_Main.Main.StartLoc+arg_strFileName, false);
            try
            {
                ArrayList al = (ArrayList)arg_al.Clone();
                foreach (string strLine in al)
                {
                    tw.WriteLine(strLine);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (tw != null)
                {
                    tw.Close();
                }
            }
        }
        private void loadRec(ref ArrayList arg_al, string arg_strFileName)
        {

            LogManager.getInstance().Log("loadRec:" + arg_strFileName);
            arg_al = new ArrayList();
            TextReader tr = null;
            try
            {
                tr = new StreamReader(Form_Main.Main.StartLoc + arg_strFileName);
                string strLine = tr.ReadLine();
                while (strLine != null && strLine != "")
                {
                    arg_al.Add(strLine);
                    strLine = tr.ReadLine();
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("loadRec Error:" + arg_strFileName, ex);

            }
            finally
            {
                if (tr != null)
                {

                    tr.Close();
                }
            }
            LogManager.getInstance().Log("loadRec OK:" + arg_strFileName);

        }
        #endregion

        #region Config

        #region PND Config Value

        public uint BackLightSecs { get { return System.Convert.ToUInt32(readConfigValue("BackLightSecs")); } set { saveConfigValue("BackLightSecs", value); } }
        public uint SoundVol { get { return System.Convert.ToUInt32(readConfigValue("SoundVol")); } set { saveConfigValue("SoundVol", value); } }
        public uint BackLightLevel { get { return System.Convert.ToUInt32(readConfigValue("BackLightLevel")); } set { saveConfigValue("BackLightLevel", value); } }

        public string LoginID { get { return System.Convert.ToString(readConfigValue("LoginID")); } set { saveConfigValue("LoginID", value); } }
        public int ScreenForeColor { get { return System.Convert.ToInt32(readConfigValue("ScreenForeColor")); } set { saveConfigValue("ScreenForeColor", value); } }
        public int ScreenBackColor { get { return System.Convert.ToInt32(readConfigValue("ScreenBackColor")); } set { saveConfigValue("ScreenBackColor", value); } }
        public int ForeColor { get { return System.Convert.ToInt32(readConfigValue("ForeColor")); } set { saveConfigValue("ForeColor", value); } }
        public int BackColor { get { return System.Convert.ToInt32(readConfigValue("BackColor")); } set { saveConfigValue("BackColor", value); } }
        public int ButtonBackColor { get { return System.Convert.ToInt32(readConfigValue("ButtonBackColor")); } set { saveConfigValue("ButtonBackColor", value); } }
        public int PanelBackColor { get { return System.Convert.ToInt32(readConfigValue("PanelBackColor")); } set { saveConfigValue("PanelBackColor", value); } }
        public int WarningBackColor { get { return System.Convert.ToInt32(readConfigValue("WarningBackColor")); } set { saveConfigValue("WarningBackColor", value); } }
        public string IMEI { get { return System.Convert.ToString(readConfigValue("IMEI")); } set { saveConfigValue("IMEI", value); } }
        public string TTS_WCString { get { return System.Convert.ToString(readConfigValue("TTS_WCString")); } set { saveConfigValue("TTS_WCString", value); } }
        public string TTSOpen { get { return System.Convert.ToString(readConfigValue("TTSOpen")); } set { saveConfigValue("TTSOpen", value); } }
        public bool IsPAPAGO { get { return System.Convert.ToBoolean(readConfigValue("IsPAPAGO")); } set { saveConfigValue("IsPAPAGO", value); } }

        
        private bool IsTTSEnable(string arg_str) { if (TTSOpen.IndexOf(arg_str) != -1) return true; return false; }

        public bool IsWCTTS { get { return IsTTSEnable("WC"); } }
        public bool IsB1TTS { get { return IsTTSEnable("B1"); } }
        public bool IsB2TTS { get { return IsTTSEnable("B2"); } }
        public bool IsM1TTS { get { return IsTTSEnable("M1"); } }
        public bool IsMRTTS { get { return IsTTSEnable("MR"); } }

        #endregion

        #region Server Config Value

        /// <summary>
        /// 排班站出車預定分鐘數, 影響到定點後, 未見及離開按鈕的出現時間
        /// </summary>
        public int StationJobMins { get { return System.Convert.ToInt32(readConfigValue("StationJobMins")); } set { saveConfigValue("StationJobMins", value); } }

        /// <summary>
        /// 約定時間後, 離開按鈕出現前多等候的分鐘數
        /// </summary>
        public int CancelExtendMins { get { return System.Convert.ToInt32(readConfigValue("CancelExtendMins")); } set { saveConfigValue("CancelExtendMins", value); } }

        /// <summary>
        /// 約定時間後, 未見按鈕出現前多等候的分鐘數
        /// </summary>
        public int MissPnrExtendMins { get { return System.Convert.ToInt32(readConfigValue("MissPnrExtendMins")); } set { saveConfigValue("MissPnrExtendMins", value); } }

        /// <summary>
        /// 任務詢問時的等候秒數
        /// </summary>
        public int WaitTimeoutSecs { get { return System.Convert.ToInt32(readConfigValue("WaitTimeoutSecs")); } set { saveConfigValue("WaitTimeoutSecs", value); } }


        /// <summary>
        /// 任務詢問時的等候秒數
        /// </summary>
        public int MsgRingSecs { get { return System.Convert.ToInt32(readConfigValue("MsgRingSecs")); } set { saveConfigValue("MsgRingSecs", value); } }

        /// <summary>
        /// 任務詢問時的等候秒數
        /// </summary>
        public int JobRingSecs { get { return System.Convert.ToInt32(readConfigValue("JobRingSecs")); } set { saveConfigValue("JobRingSecs", value); } }


        /// <summary>
        /// 任務詢問時, 前幾秒按鈕無作用
        /// </summary>
        public int JobEventBtnNextSecs { get { return System.Convert.ToInt32(readConfigValue("JobEventBtnNextSecs")); } set { saveConfigValue("JobEventBtnNextSecs", value); } }

        /// <summary>
        /// 預約任務時間到前幾分鐘開始提示
        /// </summary>
        public int ReserveJobAlarmMins { get { return System.Convert.ToInt32(readConfigValue("ReserveJobAlarmMins")); } set { saveConfigValue("ReserveJobAlarmMins", value); } }

        /// <summary>
        /// APNCODE
        /// </summary>
        public string APNCODE { get { return System.Convert.ToString(readConfigValue("APNCODE")); } set { saveConfigValue("APNCODE", value); } }

        /// <summary>
        /// 狀況回報的選單內容
        /// </summary>
        public string SReportMenu { get { return System.Convert.ToString(readConfigValue("SReportMenu")); } set { saveConfigValue("SReportMenu", value); } }

        /// <summary>
        /// 主機1的連線IP,Port
        /// </summary>
        public string ServerIP1 { get { return System.Convert.ToString(readConfigValue("ServerIP1")); } set { saveConfigValue("ServerIP1", value); } }

        /// <summary>
        /// 主機2的連線IP,Port
        /// </summary>
        public string ServerIP2 { get { return System.Convert.ToString(readConfigValue("ServerIP2")); } set { saveConfigValue("ServerIP2", value); } }


        /// <summary>
        /// 即時派遣按鈕分鐘數
        /// </summary>
        /// <param name="arg_strName"></param>
        /// <returns></returns>
        public string RealTimeButtonMins { get { return System.Convert.ToString(readConfigValue("RealTimeButtonMins")); } set { saveConfigValue("RealTimeButtonMins", value); } }

        /// <summary>
        /// 支援派遣按鈕分鐘數
        /// </summary>
        /// <param name="arg_strName"></param>
        /// <returns></returns>
        public string SupportButtonMins { get { return System.Convert.ToString(readConfigValue("SupportButtonMins")); } set { saveConfigValue("SupportButtonMins", value); } }

        public bool IsNaviEnable {get { return System.Convert.ToBoolean(readConfigValue("IsNaviEnable")); } set { saveConfigValue("IsNaviEnable", value); }  }
        public string NaviClassName { get { return System.Convert.ToString(readConfigValue("NaviClassName")); } set { saveConfigValue("NaviClassName", value); } }
        public string NaviWindowName { get { return System.Convert.ToString(readConfigValue("NaviWindowName")); } set { saveConfigValue("NaviWindowName", value); } }


        public bool IsBillEnable { get { return System.Convert.ToBoolean(readConfigValue("IsBillEnable")); } set { saveConfigValue("IsBillEnable", value); } }

        /// <summary>
        /// 是否為Debug Mode
        /// </summary>
        /// <param name="arg_strName"></param>
        /// <returns></returns>
        public bool IsDebug { get { return System.Convert.ToBoolean(readConfigValue("IsDebug")); } set { saveConfigValue("IsDebug", value); } }

        public int SOSDisableSecs { get { return System.Convert.ToInt32(readConfigValue("SOSDisableSecs")); } set { saveConfigValue("SOSDisableSecs", value); } }


        #endregion 

        /// <summary>
        /// 設定狀況回報選項, 01車子沒油
        /// </summary>
        /// <param name="arg_strData"></param>
        public void setSReportItem(string arg_strData)
        {
            try
            {
                int n = System.Convert.ToInt32(arg_strData.Substring(0, 2));

                string[] strary = StoreData.getInstance().SReportMenu.Split(';');

                strary[n -1] = arg_strData.Substring(0, 2) + "." + arg_strData.Substring(2);

                string strTemp = "";
                foreach (string str in strary)
                {
                    strTemp += str + ";";
                }
                strTemp = strTemp.TrimEnd(';');
                StoreData.getInstance().SReportMenu = strTemp;

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("參數更新失敗:"+arg_strData);
            }

        }

        public string readConfigValue(string arg_strName)
        {
            if (m_hashConfig == null)
            {
                loadConfig();
            }
            
            if (m_hashConfig.Contains(arg_strName))
            {
                return (string)m_hashConfig[arg_strName];
            }
            string strResult = "0";
            switch (arg_strName)
            {
                case "BackLightLevel":
                    strResult = "55";
                    break;
                case "SoundVol":
                    strResult = "3435973836";
                    break;
                case "StationJobMins":

                    strResult = "6";
                    break;
                case "WaitTimeoutSecs":

                    strResult = "8";
                    break;
                case "MsgRingSecs":
                    strResult = "15";
                    break;
                case "JobRingSecs":
                    strResult = "20";
                    break;

                case "BackLightSecs":

                    strResult = "30";
                    break;
                case "CancelExtendMins":

                    strResult = "3";
                    break;
                case "JobEventBtnNextSecs":

                    strResult = "60";
                    break;
                case "MissPnrExtendMins":
                    strResult = "3";
                    break;
                case "LoginID":

                    strResult = "";
                    break;
                case "APNCODE":

                    strResult = "internet";
                    break;
                case "SReportMenu":                    
                    strResult = "01.站區空;02.手動排班;03.地址錯;04.議錯價;05.取消嗎?;06.車故障;07.警察趕;08.巷口單;09.巷口窄;10.巷口障;11.車禍人傷;12.是;13.否";
                    break;
                case "ReserveJobAlarmMins":

                    strResult = "40";
                    break;

                case "RealTimeButtonMins":
                    strResult = "003006009012015020";
                    break;
                case "SupportButtonMins":
                    strResult = "003006009012015020025030040";
                    break;
                case "ServerIP1":
                    //strResult = "60.251.127.110,9113";
                    strResult = "60.250.144.69,9113";
                    break;
                case "ServerIP2":
                    //strResult = "60.251.127.110,9113";
                    strResult = "60.251.144.67,9113";
                    break;
                case "BackColor":

                    strResult = System.Convert.ToString(0x010101);
                    break;
                case "ButtonBackColor":

                    strResult = System.Convert.ToString(0x090909);
                    break;
                case "PanelBackColor":

                    strResult = System.Convert.ToString(0x010101);
                    break;
                case "ForeColor":

                    strResult = System.Convert.ToString(0xFFFFFF);
                    break;
                case "WarningBackColor":

                    strResult = System.Convert.ToString(0x300000);
                    break;
                case "IsDebug":

                    strResult = "FALSE";
                    break;
                case "IsNaviEnable":

                    strResult = "TRUE";
                    break;
                case "TTSOpen":
                    strResult = "WC,B1,B2,M1,MR";
                    break;
                case "TTS_WCString":
                    strResult = "歡迎 {0} ,登入大豐衛星導航派遣系統, 祝您行車平安.";
                    break;
                case "IMEI":
                    strResult = "";
                    break;
                case "IsBillEnable":
                    strResult = "FALSE";
                    break;
                case "SOSDisableSecs":
                    strResult = "120";
                    break;
                case "NaviClassName":
                    strResult = "WCLS_PAPAGOX2";
                    break;
                case "NaviWindowName":
                    strResult = "PAPAGOX2";
                    break;
            }
            saveConfigValue(arg_strName, strResult);
            return strResult;
        }

        public void saveConfigValue(string arg_strName, object arg_objValue)
        {
            LogManager.getInstance().Log("SCV:" + arg_strName + ":" + arg_objValue);
            string strTemp = System.Convert.ToString(arg_objValue);
            m_hashConfig[arg_strName] = strTemp;
            saveConfig();
            loadConfig();
        }

        private void saveConfig()
        {
            TextWriter tw = null;
            try
            {
                tw = new StreamWriter(Form_Main.Main.StartLoc + "Config.txt", false);
                string[] straryKey = new string[m_hashConfig.Keys.Count];
                m_hashConfig.Keys.CopyTo(straryKey, 0);

                foreach (string strKey in straryKey)
                {
                    tw.WriteLine(strKey + "\t" + (string)m_hashConfig[strKey]);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if(tw != null)
                    tw.Close();
            }

        }

        private void loadConfig()
        {
            TextReader tr = null;
            try
            {
                m_hashConfig = new Hashtable();
                tr = new StreamReader(Form_Main.Main.StartLoc + "Config.txt");
                string strLine = tr.ReadLine();
                while (strLine != null && strLine != "")
                {
                    string[] straryLine = strLine.Split('\t');
                    m_hashConfig[straryLine[0]] = straryLine[1];
                    strLine = tr.ReadLine();
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if(tr != null)
                    tr.Close();
            }
        }


        #endregion 
    }
}
